package com.fantasyland.backend.Repository;

import com.fantasyland.backend.Entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
    
    /**
     * 按时间倒序查询所有帖子
     */
    List<Post> findAllByOrderByTimestampDesc();
    
    /**
     * 根据作者查询帖子
     */
    List<Post> findByAuthorOrderByTimestampDesc(String author);
    
    /**
     * 根据标题模糊查询帖子
     */
    List<Post> findByTitleContainingIgnoreCaseOrderByTimestampDesc(String title);
    
    /**
     * 查询最新的N个帖子
     */
    @Query("SELECT p FROM Post p ORDER BY p.timestamp DESC LIMIT :limit")
    List<Post> findLatestPosts(int limit);
}